2 // phpcs:ignoreFile Generic.Arrays.DisallowLongArraySyntax,MediaWiki.Usage.DirUsage.FunctionFound
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 * Check PHP Version, as well as for composer dependencies in entry points,
24 * and display something vaguely comprehensible in the event of a totally
25 * unrecoverable error.
28 class PHPVersionCheck
{
29 /* @var string The number of the MediaWiki version used */
30 var $mwVersion = '1.31';
31 var $functionsExtensionsMapping = array(
32 'mb_substr' => 'mbstring',
33 'utf8_encode' => 'xml',
34 'ctype_digit' => 'ctype',
35 'json_decode' => 'json',
37 'mime_content_type' => 'fileinfo',
41 * @var string Which entry point we are protecting. One of:
45 * - mw-config/index.php
48 var $entryPoint = null;
51 * @param string $entryPoint Which entry point we are protecting. One of:
55 * - mw-config/index.php
59 function setEntryPoint( $entryPoint ) {
60 $this->entryPoint
= $entryPoint;
64 * Returns the version of the installed php implementation.
66 * @param string $impl By default, the function returns the info of the currently installed PHP
67 * implementation. Using this parameter the caller can decide, what version info will be
68 * returned. Valid values: HHVM, PHP
69 * @return array An array of information about the php implementation, containing:
70 * - 'version': The version of the php implementation (specific to the implementation, not
71 * the version of the implemented php version)
72 * - 'implementation': The name of the implementation used
73 * - 'vendor': The development group, vendor or developer of the implementation.
74 * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
75 * - 'minSupported': The minimum version supported by MediWiki
76 * - 'upgradeURL': The URL to the website of the implementation that contains
77 * upgrade/installation instructions.
79 function getPHPInfo( $impl = false ) {
81 ( defined( 'HHVM_VERSION' ) && $impl !== 'PHP' ) ||
85 'implementation' => 'HHVM',
86 'version' => defined( 'HHVM_VERSION' ) ? HHVM_VERSION
: 'undefined',
87 'vendor' => 'Facebook',
88 'upstreamSupported' => '3.6.5',
89 'minSupported' => '3.6.5',
90 'upgradeURL' => 'https://docs.hhvm.com/hhvm/installation/introduction',
94 'implementation' => 'PHP',
95 'version' => PHP_VERSION
,
96 'vendor' => 'the PHP Group',
97 'upstreamSupported' => '5.5.0',
98 'minSupported' => '5.5.9',
99 'upgradeURL' => 'https://secure.php.net/downloads.php',
104 * Displays an error, if the installed php version does not meet the minimum requirement.
108 function checkRequiredPHPVersion() {
109 $phpInfo = $this->getPHPInfo();
110 $minimumVersion = $phpInfo['minSupported'];
111 $otherInfo = $this->getPHPInfo( $phpInfo['implementation'] === 'HHVM' ?
'PHP' : 'HHVM' );
113 !function_exists( 'version_compare' )
114 ||
version_compare( $phpInfo['version'], $minimumVersion ) < 0
116 $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
117 . " version $minimumVersion or {$otherInfo['implementation']} version "
118 . "{$otherInfo['minSupported']}, you are using {$phpInfo['implementation']} "
119 . "{$phpInfo['version']}.";
121 $longText = "Error: You might be using an older {$phpInfo['implementation']} version. \n"
122 . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
123 . " $minimumVersion or higher or {$otherInfo['implementation']} version "
124 . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
125 . " newer php executable with a different name, such as php5.\n\n";
127 // phpcs:ignore Generic.Files.LineLength
129 Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
130 {$phpInfo['implementation']}</a>.
131 {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
132 longer supported by {$phpInfo['vendor']} and will not receive
133 security or bugfix updates.
136 If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
137 you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
138 older version of MediaWiki from our website.
139 See our<a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
140 for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
144 "Supported {$phpInfo['implementation']} versions",
153 * Displays an error, if the vendor/autoload.php file could not be found.
157 function checkVendorExistence() {
158 if ( !file_exists( dirname( __FILE__
) . '/../vendor/autoload.php' ) ) {
159 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
161 $longText = "Error: You are missing some external dependencies. \n"
162 . "MediaWiki now also has some external dependencies that need to be installed\n"
163 . "via composer or from a separate git repo. Please see\n"
164 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
165 . "for help on installing the required components.";
167 // phpcs:ignore Generic.Files.LineLength
169 MediaWiki now also has some external dependencies that need to be installed via
170 composer or from a separate git repo. Please see
171 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
172 for help on installing the required components.
176 $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
181 * Displays an error, if a PHP extension does not exist.
185 function checkExtensionExistence() {
186 $missingExtensions = array();
187 foreach ( $this->functionsExtensionsMapping
as $function => $extension ) {
188 if ( !function_exists( $function ) ) {
189 $missingExtensions[] = $extension;
193 if ( $missingExtensions ) {
194 $shortText = "Installing some PHP extensions is required.";
196 $missingExtText = '';
197 $missingExtHtml = '';
198 $baseUrl = 'https://secure.php.net';
199 foreach ( $missingExtensions as $ext ) {
200 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
201 $missingExtHtml .= "<li><b>$ext</b> "
202 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
205 $cliText = "Error: Missing one or more required components of PHP.\n"
206 . "You are missing a required extension to PHP that MediaWiki needs.\n"
207 . "Please install:\n" . $missingExtText;
210 You are missing a required extension to PHP that MediaWiki
211 requires to run. Please install:
217 $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
222 * Output headers that prevents error pages to be cached.
224 function outputHTMLHeader() {
225 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ?
$_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
227 header( "$protocol 500 MediaWiki configuration Error" );
228 // Don't cache error pages! They cause no end of trouble...
229 header( 'Cache-control: none' );
230 header( 'Pragma: no-cache' );
234 * Returns an error page, which is suitable for output to the end user via a web browser.
236 * @param string $title
237 * @param string $longHtml
238 * @param string $shortText
241 function getIndexErrorOutput( $title, $longHtml, $shortText ) {
242 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
243 if ( $this->entryPoint
== 'mw-config/index.php' ) {
244 $dirname = dirname( $pathinfo['dirname'] );
246 $dirname = $pathinfo['dirname'];
249 htmlspecialchars( str_replace( '//', '/', $dirname . '/' ) .
250 'resources/assets/mediawiki.png' );
251 $shortHtml = htmlspecialchars( $shortText );
253 header( 'Content-type: text/html; charset=UTF-8' );
255 $finalOutput = <<<HTML
257 <html lang="en" dir="ltr">
259 <meta charset="UTF-8" />
260 <title>MediaWiki {$this->mwVersion}</title>
261 <style media='screen'>
264 background-color: #fff;
265 font-family: sans-serif;
282 <img src="{$encLogo}" alt='The MediaWiki logo' />
283 <h1>MediaWiki {$this->mwVersion} internal error</h1>
301 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
302 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
303 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
304 * no longer need to be).
306 * Calling this function kills execution immediately.
308 * @param string $title HTML code to be put within an <h2> tag
309 * @param string $shortText
310 * @param string $longText
311 * @param string $longHtml
313 function triggerError( $title, $shortText, $longText, $longHtml ) {
314 switch ( $this->entryPoint
) {
316 $finalOutput = $longText;
319 case 'mw-config/index.php':
320 $this->outputHTMLHeader();
321 $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
324 $this->outputHTMLHeader();
325 $finalOutput = "/* $shortText */";
328 $this->outputHTMLHeader();
329 // Handle everything that's not index.php
330 $finalOutput = $shortText;
333 echo "$finalOutput\n";
339 * Check php version and that external dependencies are installed, and
340 * display an informative error if either condition is not satisfied.
342 * @note Since we can't rely on anything, the minimum PHP versions and MW current
343 * version are hardcoded here
345 function wfEntryPointCheck( $entryPoint ) {
346 $phpVersionCheck = new PHPVersionCheck();
347 $phpVersionCheck->setEntryPoint( $entryPoint );
348 $phpVersionCheck->checkRequiredPHPVersion();
349 $phpVersionCheck->checkVendorExistence();
350 $phpVersionCheck->checkExtensionExistence();